auto.waitFor();
device.wakeUpIfNeeded();

var storage = storages.create("GongxueyunConfig");

let holidays = storage.get("holidays")
if (holidays && isWeekend()) {
    toast("今天是周末噢，不签到");
    exit();
}

let configConsole = storage.get("switchIsShowConsole");
if (configConsole) {
    console.show();
}

var currentUser = {
    "username": storage.get("username"),
    "password": storage.get("password")
}

if (!currentUser.username || !currentUser.password) {
    log("获取用户信息失败，请检查配置文件");
    exit();
}

var activityObj = {
    "安卓页": "android.widget.FrameLayout",
    "广告页": "com.smilingmobile.practice.StartMainActivity",
    "登录页": "com.smilingmobile.practice.login.UserLoginActivity",
    "主页": "com.smilingmobile.practice.moguding_3_0.main.HomeActivity",
    "签到页": "com.smilingmobile.practice.moguding_3_0.ui.sign.SignActivity",
    "签到类型页": "com.smilingmobile.practice.moguding_3_0.ui.sign.SignTypeActivity",
    "设置页": "com.smilingmobile.practice.moguding_3_0.setting.UserInfoSettingActivity"
}

if (app.launchPackage("com.smilingmobile.practice")) {
    log("正在启动工学云应用…");
} else {
    log("启动工学云应用失败…请手动打开应用…再次打开本脚本");
    exit();
}
waitForPackage("com.smilingmobile.practice", 2000)

log("开始任务！");
isFinish = false;
while (!isFinish) {
    switch (currentActivity()) {
        case activityObj["主页"]:
            // 说明已经登录了，直接签到当前账号
            log("已经登录，开始签到");
            sign();
            isFinish = true;
            break;
        case activityObj["登录页"]:
            // 说明没有登录，需要登录
            log("没有登录，开始登录");
            if (!login(currentUser)) {
                log("[登录失败]");
            } else {
                waitForActivity(activityObj["主页"], 1000)
                sign();
            }
            isFinish = true;
            break;
        case activityObj["安卓页"]:
            sleep(1000);
            break;
        case activityObj["广告页"]:
            sleep(1000);
            break;
        default:
            // 当前不是首页也不是登录页，打开首页后继续
            log("当前不是首页也不是登录页，请打开首页后继续");
            waitForActivity(activityObj['主页'], 100)
    }
}

home();
console.hide();


function loginOut() {
    // 点击我的
    id("tv_tab_title").className("android.widget.TextView").text("我的").findOne().parent().parent().click()
    // 点击设置
    id("icon_setting").findOne().click();
    waitForActivity(activityObj["设置页"], 500)
    // 点击退出登录
    id("exit_btn").findOne().click();
    id("exitBtn0").findOne().click();
}

function fixUserInfo(user) {
    let res = getUserIdAndName();
    user.userId = res.userId;
    user.trueName = res.trueName;
    user.isSign = false;
    return user;
}

function getUserIdAndName() {
    id("tv_tab_title").className("android.widget.TextView").text("我的").findOne().parent().parent().click()
    var userId = id("tv_mogu_number").findOne().text();
    var trueName = id("tv_user_name").findOne().text();
    return {
        "userId": userId,
        "trueName": trueName
    };
}


function login(user) {
    waitForActivity("com.smilingmobile.practice.login.UserLoginActivity", 1000)
    sleep(1000);
    let checkBox = id("register_agree_cb").findOne();
    if (!checkBox.checked()) {
        checkBox.click();
    }
    sleep(1000);
    id("et_login_mobile").findOne().setText(user.username);
    id("et_login_password").findOne().setText(user.password);
    id("login_btn").findOne().click();
    sleep(2000);
    return currentActivity() != activityObj["登录页"]
}

function sign() {
    // 默认当前已经在首页的学校板块了
    log("开始签到");
    sleep(1000);
    click("签到");
    waitForActivity(activityObj["签到页"], 100)
    // id("sign_type_fl").findOne().parent().parent().click()
    // waitForActivity(activityObj["签到类型页"], 100)
    // if (isStart) {
    //     click("上班");
    // } else {
    //     click("下班");
    // }
    // click("确定");
    // waitForActivity(activityObj["签到页"], 100);
    sleep(1000);
    id("sign_text_tv").waitFor();
    log("点击签到");
    sleep(1000);
    textEndsWith("班签到").findOne().parent().click();
    log("签到成功…准备退出…");
    sleep(2000);
    back();
    sleep(1000);
    back();
    sleep(1000);
}

// 判断今天是否是周末
function isWeekend() {
    var today = new Date();
    var day = today.getDay();
    if (day == 0 || day == 6) {
        return true;
    } else {
        return false;
    }
}